package com.yy.controller.restaurantmanager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yy.common.config.RCConfig;
import com.yy.common.core.AjaxResult;
import com.yy.common.core.HttpStatus;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.utils.DateUtils;
import com.yy.common.utils.JwtHelper;
import com.yy.common.utils.file.FileUploadUtils;
import com.yy.common.utils.file.MimeTypeUtils;
import com.yy.pojo.Dish;
import com.yy.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * ClassName: DishesController
 * Package: com.yy.controller.restaurantmanager
 * Description:
 *
 * @Author yy
 * @Create 2023-12-22 12:18
 * @Version 1.0
 */
@RestController
@RequestMapping("/restaurant-manager/dishes-maintain")
public class DishesController {

    @Autowired
    private DishService dishService;

    @Autowired
    private JwtHelper jwtHelper;

    @GetMapping("query")
    public TableDataInfo queryAllSatisfiedDishes(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                 Dish dish) {
        IPage<Dish> page = dishService.getDishesList(pageNum, pageSize, dish);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    @GetMapping("{id}")
    public AjaxResult queryCertainDetailDish(@PathVariable("id") Long userId) {
        return AjaxResult.success(dishService.getById(userId));
    }

    @PostMapping("add")
    public AjaxResult addCertainDish(@RequestPart("dishPic") MultipartFile dishPic,
                                     @RequestPart("dish") Dish dish) throws Exception {
        if (!dishPic.isEmpty()) {
            String avatar = FileUploadUtils.upload(RCConfig.getDishPicPath(), dishPic, MimeTypeUtils.IMAGE_EXTENSION);
            dish.setPicture(avatar);
            boolean save = dishService.save(dish);
            if (save) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error("新增失败");
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    @PutMapping("update")
    public AjaxResult update(@RequestPart("dishPic") MultipartFile dishPic,
                             @RequestPart("dish") Dish dish,
                             @RequestHeader("token") String token) throws Exception {

        if (!dishPic.isEmpty()) {
            // 更新头像
            String avatar = FileUploadUtils.upload(RCConfig.getDishPicPath(), dishPic, MimeTypeUtils.IMAGE_EXTENSION);
            dish.setPicture(avatar);
        }
        dish.setUpdateBy(String.valueOf(jwtHelper.getUserId(token)));
        dish.setUpdateTime(DateUtils.getNowDate());
        boolean update = dishService.updateById(dish);
        if (update) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @DeleteMapping("{ids}")
    public AjaxResult delete(@PathVariable List<Long> ids) {
        boolean remove = dishService.removeByIds(ids);
        if (remove) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

}
